<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/7/27
 * Time: 14:56
 */

namespace Biz\module\Classes\adm;

use Biz\model\activity\AllActivityGoods as mAllActivityGoods;
use Biz\model\activity\AllActivitySku as mAllActivitySku;
use Biz\cache\AllActivityStock as cAllActivityStock;

/*
 *
 * 该类禁止继承,如需使用,请调用Biz\service\activity\AllActivityInfo的方法
 *
 * **/
final class AllActivityInfo
{

    public static function admUpdateActivitySkuInfo($activityId, $typeId, $activitySkuData)
    {
        foreach ($activitySkuData as $skuInfo)
        {
            $result = mAllActivitySku::updateActivitySku($activityId, $typeId, $skuInfo);

            if(!$result) throw new \Exception('修改活动汇总表sku价格失败');

            if (!empty($skuInfo['is_activity_stock']) && isset($skuInfo['activity_stock'])) {
                cAllActivityStock::setActivityStock($activityId, $typeId, $skuInfo['goods_id'], [$skuInfo['sku_id'] => $skuInfo['activity_stock']]);
            }
        }
    }

    /*
     * 编辑活动
     * **/
    public static function admUpdateActivityInfo($activityId, $typeId, $activityGoodsData, $activitySkuData, $isDel)
    {
    	$goodsIds = [];
        foreach ($activityGoodsData as $goodsInfo)
        {
            $goodsIds[] = mAllActivityGoods::updateActivityGoods($activityId, $typeId, $goodsInfo);
            if(!array_filter($goodsIds)) {
                return false;
            }
        }

        if($goodsIds && $isDel == true) {
            mAllActivityGoods::delNotInGoodsIds($activityId, $typeId, $goodsIds);
        }

        $skuIds = [];
        foreach ($activitySkuData as $skuInfo)
        {
            $skuIds[]   = mAllActivitySku::updateActivitySku($activityId, $typeId, $skuInfo);
            if(!array_filter($skuIds)) {
                return false;
            }
        }

        if ($skuIds && $isDel == true) {
            mAllActivitySku::delNotInGoodsIds($activityId, $typeId, $skuIds);
        }

        if ($goodsIds && $skuIds) {
            // cache
            foreach ($activitySkuData as $info)
            {
                if(isset($info['is_activity_stock']) && $info['is_activity_stock'] == 1 && isset($info['activity_stock']) && $info['activity_stock']) {
                    cAllActivityStock::setActivityStock($activityId, $typeId, $info['goods_id'], [$info['sku_id'] => $info['activity_stock']]);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /*
     * 删除活动
     * **/
    public static function admDelActivityInfo($activityId, $typeId)
    {
        $goodsResult = mAllActivityGoods::delActivityGoods($activityId, $typeId);
        $skuResult   = mAllActivitySku::delActivitySku($activityId, $typeId);

        if ($goodsResult && $skuResult) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 删除活动goods
     * **/
    public static function admDelActivityByGoodsId($activityId, $typeId, $goodsId)
    {
        $goodsResult = mAllActivityGoods::delActivityGoodsById($activityId, $typeId, $goodsId);
        $skuResult = mAllActivitySku::delActivitySkuById($activityId, $typeId, $goodsId);

        if ($goodsResult && $skuResult) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 删除活动sku
     * **/
    public static function admDelActivityBySkuId($activityId, $typeId, $goodsId, $skuId)
    {
        $skuResult = mAllActivitySku::delActivitySkuBySkuId($activityId, $typeId, $skuId);

        if($skuResult) {
            $goodsResult = mAllActivitySku::getActivityInfoByGoodsId($activityId, $typeId, $goodsId);
            if(!$goodsResult) {
                mAllActivityGoods::delActivityGoodsById($activityId, $typeId, $goodsId);
            }
            return true;
        } else {
            return false;
        }
    }

    public static function admUpdateActivityAttr($activityId, $typeId, array $activityAttr)
    {
        foreach ($activityAttr as $attr) {
            $result = mAllActivityGoods::updateActivity($activityId, $typeId, $attr);
            if($result == false) {
                return false;
            }

            $result = mAllActivitySku::updateActivity($activityId, $typeId, $attr);
            if($result == false) {
                return false;
            }
        }
        return true;
    }
}